package com.jade.linkong.controller;

import com.jade.common.annotation.Log;
import com.jade.common.core.controller.BaseController;
import com.jade.common.core.domain.AjaxResult;
import com.jade.common.core.domain.Ztree;
import com.jade.common.core.page.TableDataInfo;
import com.jade.common.enums.BusinessType;
import com.jade.common.utils.poi.ExcelUtil;
import com.jade.framework.util.ShiroUtils;
import com.jade.linkong.domain.LkMatter;
import com.jade.linkong.service.ILkMatterService;
import com.jade.linkong.service.impl.LkMatterTypeServiceImpl;
import com.jade.system.domain.SysDept;
import com.jade.system.domain.SysUser;
import com.jade.system.service.ISysUserService;
import com.jade.system.service.impl.SysDeptServiceImpl;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 事项Controller
 * 
 * @author jade
 * @date 2020-07-13
 */
@Controller
@RequestMapping("/linkong/matter")
public class LkMatterController extends BaseController
{
    private String prefix = "linkong/matter";

    @Autowired
    private ISysUserService userService;
    @Autowired
    private ILkMatterService lkMatterService;
    @Autowired
    private SysDeptServiceImpl deptService;
    @Autowired
    private LkMatterTypeServiceImpl lkMatterTypeService;

    @RequiresPermissions("linkong:matter:view")
    @GetMapping()
    public String matter()
    {
        return prefix + "/matter";
    }

    /**
     * 查询事项列表
     */
    @RequiresPermissions("linkong:matter:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(LkMatter lkMatter)
    {
        startPage();
        List<LkMatter> list = lkMatterService.selectLkMatterList(lkMatter);
        return getDataTable(list);
    }

    /**
     * 导出事项列表
     */
    @RequiresPermissions("linkong:matter:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(LkMatter lkMatter)
    {
        List<LkMatter> list = lkMatterService.selectLkMatterList(lkMatter);
        ExcelUtil<LkMatter> util = new ExcelUtil<LkMatter>(LkMatter.class);
        return util.exportExcel(list, "matter");
    }

    /**
     * 新增事项
     */
    @GetMapping("/add")
    public String add(ModelMap map)
    {
    	 List<SysUser> sysUsers = userService.selectByRoleId();
         map.addAttribute("comUsers", sysUsers);
        return prefix + "/add";
    }

    /**
     * 新增保存事项
     */
    @RequiresPermissions("linkong:matter:add")
    @Log(title = "事项", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(LkMatter lkMatter)
    {
        return toAjax(lkMatterService.insertLkMatter(lkMatter));
    }

    /**
     * 修改事项
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        LkMatter lkMatter = lkMatterService.selectLkMatterById(id);
        if (lkMatter.getOrgStatus().equals("0")){
            lkMatter.setCompanyId(lkMatter.getMainOrg());
            lkMatter.setCompanyName(lkMatter.getMainOrgname());
        }else if (lkMatter.getOrgStatus().equals("1")){
            lkMatter.setDeptTreeId(lkMatter.getMainOrg());
            lkMatter.setDeptName(lkMatter.getMainOrgname());
        }
        mmap.put("lkMatter", lkMatter);
        List<SysUser> sysUsers = userService.selectByRoleId();
        mmap.addAttribute("comUsers", sysUsers);
        return prefix + "/edit";
    }

    /**
     * 事项指南
     */
    @GetMapping("/guide/{id}")
    public String guide(@PathVariable("id") Long id, ModelMap mmap)
    {
        LkMatter lkMatter = lkMatterService.selectLkMatterById(id);
        mmap.put("lkMatter", lkMatter);
        return prefix + "/guide";
    }

    /**
     * 修改保存事项
     */
    @RequiresPermissions("linkong:matter:edit")
    @Log(title = "事项", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(LkMatter lkMatter)
    {
    	lkMatter.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(lkMatterService.updateLkMatter(lkMatter));
    }

    /**
     * 删除事项
     */
    @RequiresPermissions("linkong:matter:remove")
    @Log(title = "事项", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(lkMatterService.deleteLkMatterByIds(ids));
    }

    /**
     * 选择领导人树
     */
    @GetMapping("/selectDeptUserTree/{deptUserId}")
    public String selectDeptUserTree(@PathVariable("deptUserId") Long deptUserId, ModelMap mmap)
    {

        return prefix + "/tree";
    }

    /**
     * 加载领导人列表树
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = userService.selectDeptTree(new SysDept());
        return ztrees;
    }
    
    /**
     * 选择部门树
     */
    @GetMapping("/selectDeptTree/{deptId}")
    public String selectDeptTree(@PathVariable("deptId") Long deptTreeId, ModelMap mmap)
    {
        mmap.put("dept", deptService.selectDeptById(deptTreeId));
        return prefix + "/deptTree";
    }
    
    /**
     * 选择分类树
     */
    @GetMapping("/selectTypeTree/{typeTreeId}")
    public String selectTypeTree(@PathVariable("typeTreeId") Long typeTreeId, ModelMap mmap)
    {
        mmap.put("type", lkMatterTypeService.selectLkMatterTypeById(typeTreeId));
        return prefix + "/typeTree";
    }
    

}
